#!/usr/bin/env bash

CURRENT_PATH=`pwd`
set -e

QT_CFG=''

BUILD_QT=0
PROTO_BUILD=0
CLEAN=0
COMPILE_JOBS=1
MAKEFLAGS_JOBS=''

if [[ "$MAKEFLAGS" != "" ]]; then
  MAKEFLAGS_JOBS=$(echo $MAKEFLAGS | egrep -o '\-j[0-9]+' | egrep -o '[0-9]+')
fi

if [[ "$MAKEFLAGS_JOBS" != "" ]]; then
  # user defined number of jobs in MAKEFLAGS, re-use that number
  COMPILE_JOBS=$MAKEFLAGS_JOBS
elif [[ $OSTYPE = darwin* ]]; then
   # We only support modern Mac machines, they are at least using
   # hyperthreaded dual-core CPU.
   COMPILE_JOBS=4
elif [[ $OSTYPE == freebsd* ]]; then
   COMPILE_JOBS=`sysctl -n hw.ncpu`
else
   CPU_CORES=`grep -c ^processor /proc/cpuinfo`
   if [[ "$CPU_CORES" -gt 1 ]]; then
       COMPILE_JOBS=$CPU_CORES
   fi
fi

if [[ "$COMPILE_JOBS" -gt 8 ]]; then
   # Safety net.
   COMPILE_JOBS=8
fi

SILENT=''

until [ -z "$1" ]; do
    case $1 in
        "--qt-config")
            shift
            QT_CFG=" $1"
            shift;;
        "--qmake-args")
            shift
            QMAKE_ARGS=$1
            shift;;
        "--jobs")
            shift
            COMPILE_JOBS=$1
            shift;;
        "--build-qt")
            BUILD_QT=1
            shift;;
        "--proto")
            PROTO_BUILD=1
            shift;;
        "--clean")
            CLEAN=1
            shift;;
        "--silent")
            SILENT='--silent'
            QT_CFG+=" -silent"
            shift;;
        "--help")
            echo "Usage: $0 [--qt-config CONFIG] [--jobs NUM]"
            echo
            echo "  --confirm                   Silently confirm the build."
            echo "  --qt-config CONFIG          Specify extra config options to be used when configuring Qt"
            echo "  --jobs NUM                  How many parallel compile jobs to use. Defaults to 4."
            echo "  --silent                    Produce less verbose output."
            echo
            exit 0
            ;;
        *)
            echo "Unrecognised option: $1"
            exit 1;;
    esac
done

if [ $BUILD_QT -eq 1 ]; then

    echo
    echo "Building spiderkit. Please wait..."
    echo

    UNAME_SYSTEM=`(uname -s) 2>/dev/null`  || UNAME_SYSTEM=unknown
    UNAME_RELEASE=`(uname -r) 2>/dev/null` || UNAME_RELEASE=unknown
    UNAME_MACHINE=`(uname -m) 2>/dev/null` || UNAME_MACHINE=unknown

    echo "System architecture... ($UNAME_SYSTEM $UNAME_RELEASE $UNAME_MACHINE)"
    echo
    cd src/qt && ./preconfig.sh --jobs $COMPILE_JOBS --qt-config "$QT_CFG" $SILENT && cd ../..
fi

if [ $PROTO_BUILD -eq 1 ]; then
    $CURRENT_PATH/script/target/bin/protoc -I=$CURRENT_PATH/./src/kit/management/ --cpp_out=$CURRENT_PATH/./src/kit/management/ $CURRENT_PATH/./src/kit/management/message_struct.proto
    $CURRENT_PATH/script/target/bin/protoc -I=$CURRENT_PATH/./src/kit/management/ --cpp_out=$CURRENT_PATH/./src/client/ $CURRENT_PATH/./src/kit/management/message_struct.proto
    $CURRENT_PATH/script/target/bin/protoc -I=$CURRENT_PATH/./src/kit/management/ --python_out=$CURRENT_PATH/./src/python-client/ $CURRENT_PATH/./src/kit/management/message_struct.proto
    $CURRENT_PATH/script/target/bin/protoc -I=$CURRENT_PATH/./src/kit/management/ --java_out=$CURRENT_PATH/./src/java/src/skit-client/src/main/java/ $CURRENT_PATH/./src/kit/management/message_struct.proto
    $CURRENT_PATH/script/target/bin/protoc -I=$CURRENT_PATH/./src/kit/management/ --java_out=$CURRENT_PATH/./src/monitor/src/spiderkit-monitor/src/main/java/ $CURRENT_PATH/./src/kit/management/message_struct.proto
fi

if [ $CLEAN -eq 1 ]; then
    rm src/build/*.o
    rm src/build/*.cpp
    rm src/build/webpage.moc
    rm src/build/Makefile.spiderkit
    rm Makefile
    rm bin -rf
    exit 0
fi

echo "Building main spiderkit application. Please wait..."
echo

$CURRENT_PATH/src/qt/bin/qmake $QMAKE_ARGS
make -j$COMPILE_JOBS
